import ErrorCode from "../script/ErrorCode";
import { Toast } from "vant";
import {getUrlParams} from "../script/fn";
const { projectSource } = getUrlParams();
let yngpPath='http://www.zx0093.com'
import { CLIENT } from "@/common/const";
let path =CLIENT
export const register = {
  created() {
  },
  methods: {
    link() {
      if(projectSource==7){
        window.prompt(
        "",
        `up://["open.web",${Date.now()},{"type":"register.protocol","data":{"url":"${yngpPath}/yct/yngpprotocol.html","title":"约牛股票用户服务协议","popuptype":1}}]`
        // `up://["open.web",${Date.now()},{"type":"register.protocol","data":{"url":"${path}/client/yngpprotocol.html","title":"约牛股票用户服务协议","popuptype":1}}]`
      );
      }else{
        window.prompt(
        "",
        `up://["open.web",${Date.now()},{"type":"register.protocol","data":{"url":"${path}/client/protocol.html","title":"天龙博弈智能决策平台用户服务协议","popuptype":1}}]`
      );
      }
      
    },
    toggleType(num) {
      if (this.urlParams.type !== undefined && this.urlParams.type == 1) {
        window.prompt(
          "",
          `up://["web_user_login",${Date.now()},{"type":"loginprompt","data":{}}]`
        );
        return;
      }
      this.$emit("toggleType", num);
    },
    initialize() {
      this.send.phoneNumber = "";
      this.send.phoneAuthCode = "";
      this.send.password = "";
    },
    confirm(authCode) {
      const self = this;
      if (!navigator.onLine) return Toast("网络连接失败，请检查网络设置");
      this.$store
        .dispatch("sendPhoneAuthcode", {
          phoneNumber: this.$fn.encryptByDES(this.send.phoneNumber),
          imageAuthcode: authCode,
        })
        .then(function(data) {
          console.log(data);
          if (data.status == 1) {
            return self.countDown();
          }
          self.showError(ErrorCode[data.status]);
          self.hint("success", "验证码已发送至" + self.send.phoneNumber);
        });
    },
    showError(val) {
      this.hint("errors", val);
    },
    // cancel() {},
    submit() {
      const self = this;
      if (!this.$validate.phone.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.phone.error);
      }
      if (!this.$validate.notEmpty.regex.test(this.send.phoneAuthCode)) {
        return this.hint("errors", this.$validate.notEmpty.error);
      }
      if (!this.$validate.password.regex.test(this.send.password)) {
        return this.hint("errors", this.$validate.password.error);
      }
      if (!navigator.onLine) return Toast("网络连接失败，请检查网络设置");
      this.$store
        .dispatch("register", {
          phoneNumber: this.$fn.encryptByDES(this.send.phoneNumber),
          phoneAuthCode: this.send.phoneAuthCode,
          password: this.$fn.encryptByDES(this.send.password),
          checkCode: this.urlParams.version,
          versionCode: this.urlParams.buildinfo,
          projectSource:projectSource?projectSource:'3'
        })
        .then(function(data) {
          if (data.status == 1) {
            if (self.urlParams.type !== undefined && self.urlParams.type == 1) {
              window.prompt(
                "",
                `up://["web_user_login",${Date.now()},{"type":"register","data":{}}]`
              );
            } else {
              self.gologin = true;
              self.rockon();
            }
          } else {
            self.showError(ErrorCode[data.status]);
          }
        });
    },
    //提示语
    hint(status, txt) {
      this[status] = txt;
      const self = this;
      setTimeout(() => {
        self[status] = "";
      }, 1000);
    },
    getAuthCode() {
      if (!this.$validate.notEmpty.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.notEmpty.error);
      }
      if (!this.$validate.phone.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.phone.error);
      }
      this.authShow = !this.authStatus;
    },
    rockon() {
      const self = this,
        timer = setInterval(() => {
          if (self.reciprocal == 1) {
            clearInterval(timer);
            return self.toggleType();
          }
          self.reciprocal--;
        }, 1000);
    },
    countDown() {
      const TIME_COUNT = 59,
        self = this;
      let timer;
      if (!timer) {
        let second = TIME_COUNT;
        this.authStatus = 1;
        self.count = "60s重新获取";
        timer = setInterval(() => {
          if (second > 0 && second <= TIME_COUNT) {
            self.count = second-- + "s重新获取";
          } else {
            clearInterval(timer);
            timer = null;
            self.authStatus = 0;
          }
        }, 1000);
      }
    },
  },
};
